package com.dankegongyu.component.jsbridge.sdk;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import android.text.TextUtils;

import com.dankegongyu.component.jsbridge.JsBridgeConstants;
import com.dankegongyu.component.jsbridge.WebActivity;

public class JsRouter {
    public static final int REQUEST_CODE_FOR_WEB = 9999;

    public static void open(Context context, String key) {
        open(context, key, 0, 0);
    }

    public static void open(Context context, String key, int enterAnim, int exitAnim) {
        open(context, key, enterAnim, exitAnim, null);
    }

    public static void open(Context context, String key, int enterAnim, int exitAnim, Bundle bundle) {
        if (context == null || TextUtils.isEmpty(key)) {
            return;
        }
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(JsBridgeConstants.Router.ROUTER_PARAM_WEB_SERVICE_KEY, key);
        if (!(context instanceof Activity)) {    // Non activity, need less one flag.
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        if (bundle == null) {
            if (context instanceof Activity) {
                ActivityCompat.startActivityForResult((Activity) context, intent, REQUEST_CODE_FOR_WEB, new Bundle());
            } else {
                ActivityCompat.startActivity(context, intent, new Bundle());
            }
        } else {
            if (context instanceof Activity) {
                ActivityCompat.startActivityForResult((Activity) context, intent, REQUEST_CODE_FOR_WEB, bundle);
            } else {
                ActivityCompat.startActivity(context, intent, bundle);
            }
        }

        if ((context instanceof Activity) && enterAnim > 0 && exitAnim > 0) {
            ((Activity) context).overridePendingTransition(enterAnim, exitAnim);
        }
    }

}
